<%inherit file="/base/siteadmin.html"/>
<table id="grid_siteadmin_summary" cellspacing="0" summary="An summary of the 
        Grid computational resources the site admin ${c.user_name} ${c.user_surname}  may be
        allowed to access">
            <caption>
                ${config['gridname']} Information about clusters of site administrator ${c.user_name} ${c.user_surname}.
            </caption>
            <tr>
                <th class=" gm corner_left corner_right" colspan=3 scope="col" align=center > Information about cluster(s) on which you have
         siteadmin (view) permission.</th>
            </tr>

% for hostname in c.siteadmin_clusters:
<% 
id_tag = hostname.replace('.','-1-')
first = True
%>
    %if first == True:
        <tr class='table_row_emph' align='center'>
    %else:
        <tr class= 'table_row_emph' style="border-top-style: double;" >
        <% first = False %>
    %endif
        <td  colspan=3> ${c.display_names[hostname]} (${hostname}) </td>
        </tr>

        <tr class='nohover' id="${id_tag}" >
        <!-- CPU load -->
        <td><div id= "${id_tag + '_cpu'}"></div></td>
        <!-- Queue load -->
        <td><div id= "${id_tag + '_queue'}"></div></td>
        <td id= "${id_tag + '_table'}"></td>
    </tr>
    <tr class='nohover'> 
        <td colspan=3> 
            <a href="/public/rrd/${hostname}" target="_blank">
            <img src="/rrd/${hostname}stats_ch24.png" alt="CPU/Cores usage on ${hostname}"> </a>
        </td>
    </tr>
    <tr class='nohover'> 
        <td colspan=3>
            <a href="/public/rrd/${hostname}" target="_blank">
             <img src="/rrd/${hostname}stats_qh24.png" alt="Queue backlog on ${hostname}"> </a>
        </td> 
    </tr>
    <tr class='nohover'>
        <td colspan=3>
            <a href="/public/rrd/${hostname}" target="_blank">
            <img src="/rrd/${hostname}_h24r.png" alt="Response time of GRIS: ${hostname}">
            <img src="/rrd/${hostname}_h24p.png" alt="Processing time of GRIS: ${hostname}"> </a>
        </td>
    </tr>
% endfor 
            
</table>



<script language="javascript"  type="text/javascript">

google.load("visualization", "1", {
            packages : ["corechart"]
});

function drawCPUChart(hostname, tag) {
    var data, options, chart;

    var json_str = $.ajax({        
        url : '/json/cluster/gc_cpu_load/'+ hostname,
        type : 'post',
        dataType: 'text',
        async: false
    }).responseText;


    if (json_str === 'SchedduledDown'){
       $('#' + tag).html('<div class="ok_status"> Scheduled Downtime </div>');
    }
    else{
        data = new google.visualization.DataTable(json_str, 0.6);

        options = {
            axisTitlesPosition : 'none',
            backgroundColor : '#ffffff',
            colors : ['#ffa500', '#055670', '#4682b4'],
            width : 250,
            height : 75,
            chartArea:{left:2},

            hAxis : {
                baseline: 0,
            },
            isStacked : true,
            legend : 'none',
            title: 'CPU Usage of ' + hostname
        };

        chart = new google.visualization.BarChart(document.getElementById(tag));
        chart.draw(data, options);
    }
}

function drawQueueChart(hostname, tag) {
    var data, options, chart;


    var json_str = $.ajax({
        url : '/json/cluster/gc_queue_load/' + hostname,
        type : 'post',
        dataType: 'text',
        async: false
    }).responseText;

    if (json_str === 'NoQueueError'){
       $('#' + tag).html('<div class="error_status"> No Queue </div>');
    }
    else if (json_str === 'SchedduledDown'){
       $('#' + tag).html('<div class="ok_status"> Scheduled Downtime </div>');
    }
    else{
        data = new google.visualization.DataTable(json_str, 0.6);

        var options = {
            axisTitlesPosition : 'none',
            backgroundColor : '#ffffff',
            colors : ['#ffa500', '#055670', '#4682b4'],
            width : 250,
            height : 75,
            chartArea:{left:45},
            hAxis:{
                baseline: 0
            },
            isStacked : true,
            legend : 'none',
            title: 'Queue backlog on ' + hostname
        };

        var chart = new google.visualization.BarChart(document.getElementById(tag));
        chart.draw(data, options);
    }
}
   

var generateLoadTable = function(hostname, tag){
    var  _url = '/json/cluster/get_cluster_load/' + hostname;
    $.ajax({
        url: _url,
        type: 'POST',
        dataType: 'json',
        success: function(data){
            // build table
            _html = '<table summary="Cluster load infobox" class="load_infobox" >' +
                    '<tbody> <tr class="load_infobox"> <th class="gm" scope ="col">Name</th>' +                 
                    '<th class="gm" scope="col">Gridrun</th>' +
                    '<th class="gm" scope="col" >Run</th>' +
                    '<th class="gm" scope="col" >Gridq</th>' +
                    '<th class="gm" scope="col" >Localq</th>' +
                    '<th class="gm" scope="col" >Lrmsq</th> </tr>';

            // for each queue
            for(var qname in data['q']){
                _html += '<tr> <td>' + qname  + '</td>' +
                         '<td> ' + data['q'][qname]['grid_running'] + '</td>' +
                         '<td> ' + data['q'][qname]['running'] + '</td>' +
                         '<td> ' + data['q'][qname]['grid_queued'] + '</td>' +
                         '<td> ' + data['q'][qname]['local_queued'] + '</td>' +
                         '<td> ' + data['q'][qname]['prelrms_queued'] + '</td></tr>';
            }

            // sum of jobs
            _html += '<tr style="border-top-style: double;">' +
                    ' <td colspan="3">running:' +  data['cl']['cl_running'] + '</td>' +
                    ' <td colspan="3">gridrunning:' +  data['cl']['cl_grid_running'] + '</td> </tr>' +
                    ' <tr> <td colspan="3"> total jobs:'+  data['cl']['cl_running'] +'/'+ data['cl']['cl_totaljobs'] + '</td>'+
                    ' <td colspan="3"> used cpus:' +  data['cl']['cl_usedcpus'] +'/' + data['cl']['cl_totalcpus'] + '</td></tr></tbody></table>';
           $('td#'+tag).html(_html);
        }
    });
}
 

function cpuQueueLoadCallback(){
    $('table#grid_siteadmin_summary tr[id]').each(function(){
        var _id = $(this).attr('id');
        var hostname = _id.replace(/-1-/g, '.');  // replacing '-1-' with '.'
        var cpu_tag = _id + '_cpu';
        var queue_tag = _id + '_queue';
        var table_tag = _id + '_table';
    
        drawCPUChart(hostname, cpu_tag);
        drawQueueChart(hostname, queue_tag);
        generateLoadTable(hostname, table_tag);
    });
}

$(document).ready(function() {
        cpuQueueLoadCallback();
});
        
</script>

<%def name="js()">        
    ${parent.js()}
    ${self.js_link("https://www.google.com/jsapi")}
</%def>

